package com.xdf.teacher.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xdf.teacher.R;
import com.xdf.teacher.appliction.MyApplication;
import com.xdf.teacher.entity.User;
import com.xdf.teacher.http.NetUtils;

import net.neiquan.applibrary.base.BaseActivity;
import net.neiquan.applibrary.numberpicker.view.AlertDatePicker;
import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.ToastUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by JavaDog on 2018-3-21.
 */

public class LoginActivity extends Activity {
    @InjectView(R.id.mAccountEt)
    EditText mAccountEt;
    @InjectView(R.id.mPwdEt)
    EditText mPwdEt;
    @InjectView(R.id.mPwdHint)
    ImageView mPwdHint;
    @InjectView(R.id.mLoginTv)
    TextView mLoginTv;
    private boolean show = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.inject(this);
        User user = MyApplication.getInstance().user;
        if(user!=null){
            User.ResultBean result = user.getResult();
            mAccountEt.setText(result.getNick_name());
            mPwdEt.setText(result.getPassword());
        }
        mPwdEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    @OnClick({R.id.mLoginTv, R.id.mPwdHint})
    public void onViewClicked(View view) {
        String account = mAccountEt.getText().toString().trim();
        String pwd = mPwdEt.getText().toString().trim();
        switch (view.getId()) {
            case R.id.mLoginTv:
                NetUtils.getInstance().login(account, pwd, new NetCallBack() {
                    @Override
                    public void onSuccess(final String response, String mssage, ResultModel model) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                AppLog.e("======onSuccess=========" + response);
                                User user = (User) JSON.parseObject(response, User.class);
                                if (user.getErrorcode() == -3) {
                                    ToastUtil.shortShowToast(user.getErrormsg());
                                } else {
                                    AppLog.e("======user=========" + user);
                                    if(user.getResult().getRole()==2){
                                        MyApplication.getInstance().saveUser(user);
                                        startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                                    }else{
                                        ToastUtil.shortShowToast("移动端只能登陆学生账号...");
                                    }
                                }
                            }
                        });
                    }

                    @Override
                    public void onFail(boolean dataError, int errorCode, String mssage) {

                    }
                }, null);
                break;
            case R.id.mPwdHint:
                show = !show;
                mPwdHint.setImageResource(show ? R.drawable.show : R.drawable.hide);
                mPwdEt.setInputType(show ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                break;
        }
    }

}
